package com.sourceQ.system.controller.message.sms;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sourceQ.common.core.web.controller.BaseController;
import com.sourceQ.common.core.web.domain.AjaxResult;
import com.sourceQ.common.log.annotation.Log;
import com.sourceQ.common.log.enums.BusinessType;
import com.sourceQ.common.security.utils.SecurityUtils;
import com.sourceQ.system.dto.message.SmsTemplatePageReqDTO;
import com.sourceQ.system.dto.message.SmsTemplateSaveReqDTO;
import com.sourceQ.system.dto.message.SmsTemplateSendReqDTO;
import com.sourceQ.system.service.message.sms.SmsSendService;
import com.sourceQ.system.service.message.sms.SysSmsTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/sms-template")
@Api(tags = "短信模板管理")
public class SmsTemplateController extends BaseController {
    @Autowired
    private SysSmsTemplateService smsTemplateService;
    @Autowired
    private SmsSendService smsSendService;
    @PostMapping("/create")
    @Log(title = "短信模板管理",businessType = BusinessType.INSERT)
    @ApiOperation("短信模板-新增")
    public AjaxResult create(@Valid @RequestBody SmsTemplateSaveReqDTO createReqVO){
        return success(smsTemplateService.createSmsTemplate(createReqVO));
    }
    @PostMapping("/update")
    @Log(title = "短信模板管理",businessType = BusinessType.UPDATE)
    @ApiOperation("短信模板-修改")
    public AjaxResult update(@Valid @RequestBody SmsTemplateSaveReqDTO updateReqVO){
        smsTemplateService.updateSmsTemplate(updateReqVO);
        return success();
    }
    @DeleteMapping("/delete/{id}")
    @Log(title = "短信模板管理",businessType = BusinessType.DELETE)
    @ApiOperation("短信模板-删除")
    public AjaxResult delete(@PathVariable Long id){
        smsTemplateService.deleteSmsTemplate(id);
        return success();
    }
    @GetMapping("/detail/{id}")
    @Log(title = "短信模板管理",businessType = BusinessType.QUERY)
    @ApiOperation("短信模板-详情")
    public AjaxResult get(@PathVariable Long id){
        return success(smsTemplateService.getSmsTemplate(id));
    }
    @GetMapping("/page")
    @Log(title = "短信模板管理",businessType = BusinessType.QUERY)
    @ApiOperation("短信模板-查询")
    public AjaxResult page(Page page, SmsTemplatePageReqDTO pageReqVO){
        return success(smsTemplateService.getSmsTemplatePage(page, pageReqVO));
    }
    @PostMapping("/send-sms")
    @ApiOperation("短信发送测试")
    @Log(title = "短信模版管理",businessType = BusinessType.OTHER)
    public AjaxResult send(@NotNull@RequestBody SmsTemplateSendReqDTO sendReqDTO){
        return success(smsSendService.sendSingleSms(sendReqDTO.getMobile(), SecurityUtils.getUserId(),"0",sendReqDTO.getTemplateCode(),sendReqDTO.getTemplateParams()));
    }
}
